package com.h3c.nem.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eos.common.util.IdWorker;
import com.h3c.nem.entity.SdNoticeRecord;
import com.h3c.nem.entity.TbOfferWorker;
import com.h3c.nem.entity.TbOfferWorkerFeedback;
import com.h3c.nem.mapper.TbOfferWorkerFeedbackMapper;
import com.h3c.nem.mapper.TbOfferWorkerMapper;
import com.h3c.nem.service.CommService;
import com.h3c.nem.service.TbOfferWorkerFeedbackService;
import com.h3c.nem.utils.AppBusinessException;
import com.h3c.nem.utils.BusiEnumDefine;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

/**
 * (TbOfferWorkerFeedback)表服务实现类
 *
 * @author makejava
 * @since 2022-07-27 14:25:11
 */
@Service("tbOfferWorkerFeedbackService")
public class TbOfferWorkerFeedbackServiceImpl extends ServiceImpl<TbOfferWorkerFeedbackMapper, TbOfferWorkerFeedback> implements TbOfferWorkerFeedbackService {
    @Resource
    private TbOfferWorkerFeedbackMapper tbOfferWorkerFeedbackMapper;

    @Resource
    private TbOfferWorkerServiceImpl tbOfferWorkerService;

    @Resource
    private TbOfferWorkerMapper tbOfferWorkerMapper;

    @Resource
    private CommService commService;

    @Value("${receiveEmails}")
    private String receiveEmail;

    /**
     * 通过ID查询单条数据
     *
     * @param feedbackId 主键
     * @return 实例对象
     */
    @Override
    public TbOfferWorkerFeedback queryById(String feedbackId) {
        return this.tbOfferWorkerFeedbackMapper.queryById(feedbackId);
    }

    @Override
    public String submitFeedback(String workerId, String feedback) {
        TbOfferWorker tbOfferWorker = tbOfferWorkerMapper.selectById(workerId);
        if ( !Objects.nonNull(tbOfferWorker)) {
            throw new AppBusinessException("该用户不存在");
        }
            TbOfferWorkerFeedback feedbackWorker = new TbOfferWorkerFeedback();
            feedbackWorker.setSubTime(new Date());
            feedbackWorker.setWorkerId(tbOfferWorker.getWorkerId());
            feedbackWorker.setWorkerName(tbOfferWorker.getWorkerName());
            feedbackWorker.setFeedback(feedback);
            String feedbackId = IdWorker.nextId() + "";
            feedbackWorker.setFeedbackId(feedbackId);
            tbOfferWorkerFeedbackMapper.insert(feedbackWorker);
            sendEmailBatch(feedback, tbOfferWorker);
            return "success";
    }



    @Override
    public void sendEmailBatch(String feedback, TbOfferWorker tbOfferWorker) {
        String name = tbOfferWorker.getWorkerName() + "/" + tbOfferWorker.getWorkerId();
        SdNoticeRecord record = new SdNoticeRecord();
        record.setNoticeId(IdWorker.nextId() + "");
        record.setFlowId("-1");
        record.setNoticeType(BusiEnumDefine.NOTICE_TYPE_EMAIL);
        record.setTargetEmail(receiveEmail.trim());
        record.setNoticeTitle(name + "提交问题反馈");
        record.setNoticeContent(name + "提交的问题反馈为: \n" + feedback + "<br>员工邮箱：" + tbOfferWorker.getEmail() + "  员工手机：" + tbOfferWorker.getPhoneNo());
        record.setSendTime(new Date());
        commService.sendMessage(record);

    }


    /**
     * 分页查询
     *
     * @param tbOfferWorkerFeedback 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<TbOfferWorkerFeedback> queryByPage(TbOfferWorkerFeedback tbOfferWorkerFeedback, PageRequest pageRequest) {
        long total = this.tbOfferWorkerFeedbackMapper.count(tbOfferWorkerFeedback);
        return new PageImpl<>(this.tbOfferWorkerFeedbackMapper.queryAllByLimit(tbOfferWorkerFeedback, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param tbOfferWorkerFeedback 实例对象
     * @return 实例对象
     */
    @Override
    public TbOfferWorkerFeedback insert(TbOfferWorkerFeedback tbOfferWorkerFeedback) {
        this.tbOfferWorkerFeedbackMapper.insert(tbOfferWorkerFeedback);
        return tbOfferWorkerFeedback;
    }

    /**
     * 修改数据
     *
     * @param tbOfferWorkerFeedback 实例对象
     * @return 实例对象
     */
    @Override
    public TbOfferWorkerFeedback update(TbOfferWorkerFeedback tbOfferWorkerFeedback) {
        this.tbOfferWorkerFeedbackMapper.update(tbOfferWorkerFeedback);
        return this.queryById(tbOfferWorkerFeedback.getFeedbackId());
    }

    /**
     * 通过主键删除数据
     *
     * @param feedbackId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String feedbackId) {
        return this.tbOfferWorkerFeedbackMapper.deleteById(feedbackId) > 0;
    }
}
